/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SwipeToRegisterFrame.java
 *
 * Created on Jan 22, 2010, 12:28:42 AM
 */

package org.acmwustl.bubble.gui;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ResourceBundle;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import javax.swing.SwingWorker;
import javax.swing.WindowConstants;
import org.acmwustl.bubble.gui.dialogs.AlreadyRegisteredFrame;
import org.acmwustl.bubble.logic.ConnectionManager;
import org.acmwustl.bubble.windowutils.WindowUtils;

/**
 *
 * @author Tyler
 */
public class SwipeToRegisterFrame extends javax.swing.JFrame
implements KeyListener {

    private boolean ignoreInput = false;
    private String keyIDCode = "";

    private String fName, lName, uName, domain, email, pword;

    /** Creates new form SwipeToRegisterFrame */
    public SwipeToRegisterFrame() {
        initComponents();

        setSize(new Dimension(1024,768));
        WindowUtils.centerWindow(this);

        getRootPane().addKeyListener(this);
        getRootPane().requestFocus();
    }

    public SwipeToRegisterFrame(String fName, String lName, String uName, String domain, String email, String pWord) throws HeadlessException {
        initComponents();

        this.fName = fName;
        this.lName = lName;
        this.uName = uName;
        this.domain = domain;
        this.email = email;
        this.pword = pWord;

        setSize(new Dimension(1024,768));
        WindowUtils.centerWindow(this);

        getRootPane().addKeyListener(this);
        getRootPane().requestFocus();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        JLabel jLabel1 = new JLabel();
        JLabel jLabel2 = new JLabel();
        JLabel jLabel3 = new JLabel();
        JButton jButton1 = new JButton();

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N
        setUndecorated(true);

        jLabel1.setFont(new Font("Tahoma", 0, 48)); // NOI18N
        jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
        ResourceBundle bundle = ResourceBundle.getBundle("org/acmwustl/bubble/gui/Bundle"); // NOI18N
        jLabel1.setText(bundle.getString("SwipeToRegisterFrame.jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setFont(new Font("Tahoma", 0, 36)); // NOI18N
        jLabel2.setText(bundle.getString("SwipeToRegisterFrame.jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setFont(new Font("Tahoma", 0, 18)); // NOI18N
        jLabel3.setText(bundle.getString("SwipeToRegisterFrame.jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jButton1.setFont(new Font("Tahoma", 0, 36)); // NOI18N
        jButton1.setText(bundle.getString("SwipeToRegisterFrame.jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(Alignment.CENTER)
                    .addComponent(jLabel1, GroupLayout.DEFAULT_SIZE, 923, Short.MAX_VALUE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jButton1, GroupLayout.PREFERRED_SIZE, 189, GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addPreferredGap(ComponentPlacement.RELATED, 287, Short.MAX_VALUE)
                .addComponent(jButton1, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        dispose();
    }


    public void keyTyped(KeyEvent e) {
        if(!ignoreInput) {
            keyIDCode += e.getKeyChar();
            if(keyIDCode.length() == 19) {
                if(keyIDCode.matches(";\\d{5}=\\d{11}\\?")) {
                    if(!ConnectionManager.getInstance().existsCardNumber(keyIDCode)) {
                        domain.substring(1, domain.length());
                        ConnectionManager.getInstance().makeNewMember(fName, lName, uName, domain, email, pword, keyIDCode);
                        dispose();
                    } else {
                        dispose();
                        new AlreadyRegisteredFrame().setVisible(true);
                    }
                }
                else {
                    keyIDCode = "";
                    new IgnoreInputTimer().execute();
                    Toolkit.getDefaultToolkit().beep();
                }
            }
        }
    }

    public void keyPressed(KeyEvent e) {
    }

    public void keyReleased(KeyEvent e) {
    }//GEN-LAST:event_jButton1ActionPerformed

    private class IgnoreInputTimer extends SwingWorker {

        private long time = 300;
        private boolean running = true;
        private long lastMillis;
        
        public IgnoreInputTimer() {
            ignoreInput = true;
        }

        @Override
        protected Object doInBackground() throws Exception {
            lastMillis = System.currentTimeMillis();
            while(running) {
                time -= (System.currentTimeMillis() - lastMillis);
                lastMillis = System.currentTimeMillis();
                if(time <= 0) {
                    ignoreInput = false;
                    running = false;
                    System.err.println("DONE!");
                }
                else
                    Thread.sleep(100);
            }
            return null;
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
